<?php
namespace Common\Model;

use Think\Model;

class SystemAreaModel extends \BaseModel
{

    public function load($id)
    {
        $data = $this->where("id='$id'")->find();
        if ($data['pid']) {
            $data['parent'] = $this->load($data['pid']);
        }
        return $data;
    }


    public function Childs($area_id)
    {
        $res = $this->where("pid='$area_id'")->cache("area_" . $area_id)->select();
        return $res;
    }


    /*****返回下一级area_ids****/
    public function ChildsIds($id)
    {
        $one1 = $this->Childs($id);
        $ids[] = $id;
        foreach ($one1 as $one) {
            $ids[] = $one['id'];
            $one1 = $this->Childs($one['id']);
            if ($one1) {
                $ids[] = $this->ChildsIds($one['id']);
            }
        }
        return implode(",", $ids);
    }


    /*****递归查找下一级ids是一个数组****/
    public function subChilds($ids)
    {
        if (!is_array($ids)) $ids = array('id' => $ids);
        foreach ($ids as $key => $one) {
            $ids[$key]['subChilds'] = $this->Childs($one['id']);
            if ($ids[$key]['subChilds']) {
                $ids[$key]['subChilds'] = $this->subChilds($ids[$key]['subChilds']);
            }
        }
        return $ids;
    }


}

?>